<extend name="Layout/ins_page" />

<block name="head">
</block>

<block name="js">


</block>

<block name="content">



    <!--content begin-->
    <style type="text/css">
        table
        {
            border-collapse:collapse; width:60%
        }
        table, td, th
        {
            border:1px solid black;
        }
        td{width:10%}
    </style>
    <script src="Public/Scripts/jquery-1.8.3.js"></script>
    <script src="Public/Scripts/timejs.js"></script>
    <script>
        function unique(arr) {
            var result = [], hash = {};
            for (var i = 0, elem; (elem = arr[i]) != null; i++) {
                if (!hash[elem]) {
                    result.push(elem);
                    hash[elem] = true;
                }
            }
            return result;
        }


        function linetoaldata(TopCount,BackMonth){
            postquery='marginaldo'+TopCount+'do'+BackMonth;
            $.ajax({
                url: "/Report/topbrandService",
                type: "POST",
                dataType: 'jsonp',
                jsonp: 'postquery',
                jsonpCallback: postquery,
                timeout: 200000,
                success: function (jsonp) {
                    //console.log(jsonp);
                    datalinetotal=jsonp;
                    for (var i=0;i<datalinetotal.length;i++)
                    {
                        linetotalbrand[i]=datalinetotal[i]['topbrand'];//加入行标题
                    }
                    linetotalbrand=unique(linetotalbrand);	//行标题去重
                    console.log(linetotalbrand);
                    for (var i=0 ; i<linetotalbrand.length; i++){
                        var trs1=document.getElementById('linetoalTable').insertRow();
                        for(var irow=0;irow<=Number(BackMonth)+1;irow++)
                        {
                            trs1.insertCell();
                        }
                    }//画数据行再画数据列
                    for (var i=0;i<linetotalbrand.length;i++)
                    {
                        document.getElementById('linetoalTable').rows[i+1].cells[0].innerHTML=linetotalbrand[i]
                    }
                    for (var i=0;i<datalinetotal.length;i++)
                    {
                        for(var imonth=0;imonth<linetotalheads.length;imonth++)
                        {
                            //alert(linetotalheads[imonth]);
                            if (datalinetotal[i]['ymonth']==linetotalheads[imonth])
                            {
                                //alert(linetotalheads[imonth]);
                                document.getElementById('linetoalTable').rows[linetotalbrand.indexOf(datalinetotal[i]['topbrand'])+1].cells[imonth+1].innerHTML=datalinetotal[i]['linetotal']
//				//console.log(datalinetotal[i]['linetotal'])
                            }
                        }
                    }
                    //console.log(datalinetotal);
                }/*对jsonp返回函数的*/
            }/*对AJAX*/);  }



    </script>

    <script>



        //echars画图用
        window.linetotalArray=[];
        window.linetotalbrand=[];
        //


        datalinetotal=[];

        function querydb(){

            var tabletitle =document.getElementById("linetoalTableHead");
            if (tabletitle.cells.length>1) {
                for ( var i = tabletitle.cells.length-1; i>0; i--)
                {
                    tabletitle.deleteCell (i);
                }		}
            var TopCount=document.getElementById('TopCount').value
            var BackMonth=document.getElementById('BackMonth').value
            datalinetotal.length=0;
            drawtable(TopCount,BackMonth);
            linetoaldata(TopCount,BackMonth);
        }

        function drawtable(TopCount,BackMonth){
            window.linetotalheads=[];
            linetotalheads.length=0;
            var tablemain =document.getElementById("linetoalTable");
            //console.log(rows);
            if ( tablemain.rows.length>1) {
                for ( var i = tablemain.rows.length-1; i>0; i--)
                {
                    tablemain.deleteRow (i);
                }		}

            for(var i=0 ; i<=BackMonth;i++){
                var ths1=document.getElementById('linetoalTableHead').insertCell()
                var m=new Date();
                var m=DateAdd('m',-i,);
                linetotalheads[i] = m.Format("yy/MM");
                ths1.innerHTML=m.Format("yy/MM");
            }//画列头
        }
    </script>
    <div>
        销售金额最小值<input id='TopCount' maxlength="6" value="50000" >
        </input>
        </select>
        回溯月份
        <input id='BackMonth' maxlength="2" value="3" ></input>
        <button onclick='querydb();'>查询</button>
    </div>
    </br>
    <div id="linetoalTableContainer">
        <table id='linetoalTable' class="tablesorter"  ><thead>
        <tr id='linetoalTableHead'>
            <td>销售金额</td>
        </tr></thead>
            <tbody></tbody>
        </table>
    </div>
    <div >
    </div>



    <!--content end-->

</block>